﻿using System;

namespace CSharp进阶_泛型约束
{
	
	class Test<T> where T : new()
	{
		
	}
	interface IA<T> where T : new()
	{
		
	}
	class Base<T> where T:new()
	{
		private static T instance = new T();
		private Base() { }
		public static T Instance { get { return instance; } }
	}

	class ArrayList<T>
	{
		private T[] array_;
		private int size_;
		private int capacity_;
		public ArrayList()
		{
			capacity_ = 4;
			size_ = 0;
			array_ = new T[capacity_];
		}
		
		public void Add(T val)
		{
			if (size_ == capacity_)
			{
				int newCapacity = Capacity * 2;
				T[] newArray = new T[newCapacity];
				for (int i = 0; i < size_; i++)
					newArray[i] = array_[i];
				array_ = newArray;
				capacity_ = newCapacity;
			}
			array_[size_++] = val;
		}
		public void Remove(int pos)
		{
			if (pos < 0 || pos >= size_) return;
			for (int i = pos; i < size_ - 1; i++)
			{
				array_[i] = array_[i + 1];
			}
			size_--;
		}
		public void Show()
		{
			for (int i = 0; i < size_; i++)
			{
				Console.Write(array_[i] + " ");
			}
			Console.WriteLine();
		}

		public T this[int pos]
		{
			get
			{
				if (pos < 0 || pos >= size_) return default(T);
				return array_[pos];
			}
			set
			{
				if (pos < 0 || pos >= size_) return;
				array_[pos] = value;
			}
		}
		public int Size { get { return size_; } }
		public int Capacity { get { return capacity_; } }
	}
	class Program
	{
		static void Main(string[] args)
		{
			ArrayList<int> list = new ArrayList<int>();
			list.Add(1);
			list.Add(3);
			list.Add(2);
			list.Add(4);
			list.Show();
			list.Remove(0);
			list.Show();
			list[0] = 1000;
			list.Show();
			//Base<int> b = Base<int>.Instance;
			//b.data_ = 1;
			//Console.WriteLine(b.data_);
			//Test<object> t1 = new Test<object>();
			//Test<int> t2 = new Test<int>();
		}
	}
}
